import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import { Row, Col } from 'antd'
import { Span } from '_platform/components4max/Span'
import { Button } from '_platform/components4max/Button'
import style from './style.css'
import { getCurrentHostPath } from './util/utils'
import LinkModal from './LinkModal';
import {getPermissions,getUser} from '_platform/auth';
import imgSrc from "./bg_digit_cim_bar.png"
import {
    setUser,
    clearUser,
    setPermissions,
    removePermissions
} from '_platform/auth';
import Timer from './Timer'
var moment = require('moment')

export default class Headmenu extends Component {
    constructor() {
        super()
        this.state = {
            isLink: false,
            linkVisible: false,
            isCollapse: true,
            // dropLink: false
        }
    }
    componentDidMount() {
        // const { props: { location: { pathname } } } = this;
        // if(pathname == '/app/planning/zg'){
        //     setTimeout(() => {
        //         this.totalPlan()
        //     },7000)
        // }
    }
    componentWillReceiveProps(nextprops){
        const { props: { location: { pathname } } } = this;
        const {location: {pathname:pathname1}} = nextprops;
        // const oldRootPath = pathname.split('/')[2];
        // const newRootPath = pathname1.split('/')[2];
        // const oldtype = pathname.split('/')[3];
        // const newtype = pathname1.split('/')[3];
        // if(window.isfullScreen){        // 点击全屏
        //     window.isfullScreen = false;
        //     return;
        // }
        // if(pathname1 == '/app/construction'){ // 点击总况
        //     this.goHome();
        //     return;
        // }
        // if(oldRootPath !== newRootPath){    
        //     switch(newRootPath){
        //         case 'planning' : this.totalPlan();break;
        //         case 'construction' : this.goHome();break;
        //         case 'operation' : this.goHome();break;
        //         default : break;
        //     }
        // }else {
        //     if(oldtype !== newtype){
        //         switch(newtype){
        //             case 'zg' : this.totalPlan();break;
        //             case 'xg' : this.detailPlan();break;
        //             default : break;
        //         }
        //     }
        // }
    }



    //显示链接
    showLinkCheck(pathname) {
        if (pathname == "/app/construction") {
            this.onLinkClick(linkData)
        } else if(pathname == "/app/operation"){
            //弹出链接
            let linkVisible = !this.state.linkVisible
            this.setState({ linkVisible,linkDataqqq:linkData2 })
        } else if(pathname == "/app/planning"){
            this.onLinkClick(linkData1)
        } else {
            //弹出链接
            let linkVisible = !this.state.linkVisible
            this.setState({ linkVisible,linkDataqqq:linkData })
        }
    }

    goHome = () => {
        const {
            __g: { htmlWindow },
            outProject,
            changeUrl,
            PlanLayersLoadData,
            __config: { initLocation },
            setLocation,
            htmlWinFactory: { hideHtml },
        } = window
        setLocation(initLocation)
        //隐藏弹出窗
        hideHtml(true, htmlWindow)
        // 隐藏图层
        outProject()
    }
    // 点击 标题 跳转
    handleTileClick = (name) => {
        const permissions = getPermissions() || [];
        console.log(this.props)
        const {history} = this.props;
        const {__g} = window;
        this.setState({ isCollapse: true })
        switch(name){
            case '规划':
                if(permissions.some(permission => permission === `appmeta.PLANNING.READ`)){
                    history.push('/app/planning/zg');
                    break;
                }
            case '建设':
                if(permissions.some(permission => permission === `appmeta.CONSTRUCTION.READ`)){
                    history.push('/app/construction');
                    break;
                }
            case '运营':
                if(permissions.some(permission => permission === `appmeta.OPERATION.READ`)){
                    history.push('/app/operation');
                    break;
                }
            case '登出':
                 __g&&__g.terminate&&__g.terminate(); 
                 CollectGarbage();
                 window.location.replace('/login');                 
                // setTimeout(() => {
                // }, 2000); 
                break;
            default: history.replace('/')
        }
        
    }
    //各个模块的点击事件,避免IE下出现链接提示
    onClick(href){
        if(!href || href =='')
            return;
        const {history} = this.props;
        history.push(href);
    }
  
    //点击链接
    onLinkClick(linkData) {
        const { setlinkLayer, htmlWinFactory: { openWindowByName }, __g: { htmlWindow } } = window
        openWindowByName('linkLayer', htmlWindow)
        setlinkLayer(linkData)
    }
    //返回导航页
    goBack(){
        const { __g } = window
        const {history} = this.props;
        __g&&__g.terminate&&__g.terminate(); 
        CollectGarbage();
        window.location.replace('/');
                // setTimeout(() => {
                    
                // }, 2000); 
    }
    
    render() {
        const { location: { pathname } } = this.props
        console.log('pathname:', pathname)
        const { isLink,linkDataqqq } = this.state;

        let id = pathname.split('/')[4]
        let type = pathname.split('/')[3]
        let rootPath = pathname.split('/')[2]
        const {username = '', name = '' } = getUser();
        return (
            <div style={{ color: '#fff', fontWeight: 'bold' }}>
                <div className={style.bar}>
                    <img style={{}} src={imgSrc} />
                </div>
                <LinkModal visible={this.state.linkVisible} dataSource={linkDataqqq} />
                <div className={style['logout']}>
                    欢迎您：{!name || name == 'null'  ? username : name}
                    {this.getLogoutRegion()}
                </div>
                <div  className={style['timer']}>
                    <Timer/>
                </div>
                <div className={style['return']} onClick={this.goBack.bind(this)}>
                    返回
                </div>
                <div className={style['linkarea']} onClick={this.showLinkCheck.bind(this,pathname)}>
                    链接
                </div>
            </div>
        )
    }

    getLogoutRegion () {
        return (
            <div className={style['logoutBtn']}>
                <a onClick={() => this.handleTileClick('登出')}>
                    <div className={style['logoutIcon']} />
                </a>
            </div>
        )
    }

}
const menuButtonStyle = {
    width: '100%',
    margin: '0 auto',
    fontSize: '26px',
}
const zg = ['建筑限高']
//const zg =["总规用地","起步区用地","建筑限高","道路交通相关专项","高速公路和城市道路","高铁和轨道交通系统","水资源保障专题研究"]
const xg = ['规划范围']
//const xg = ["设计产业复合","底商","安置商住","体育","安置住宅","非安置住宅","教育","基础设施","行政管理","规划范围","商业服务"]

const { linkData,linkData1,linkData2 } = config
const Linkable = ({children, style = {}, to, onClick, disable}) => {
    // if (disable) {
    //     return (
    //         <p style={style}>{children}</p>
    //     )
    // }
    return (
        <Link
            style={style}
            to={to}
            onClick={onClick}
        >
            {children}
        </Link>
    )
}
